package cn.pzhu.services.admin;

import cn.pzhu.model.Company;
import cn.pzhu.model.Expert;
import cn.pzhu.services.BaseServices;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by dongp on 2016/4/25.
 */
public class ExpertServices extends BaseServices {

    public List<Company> ajaxQueryPage(String pageNumber, String pageSize, String search) {
        ArrayList arrts = new ArrayList();
        Map<String,Object> map = new HashMap<>();

        List<Expert> experts = Expert.dao.find("select e.* from expert e where e.name like '%" + search + "%' limit "
                + pageNumber + "," + pageSize);
        arrts.add(experts);

        Expert expert = Expert.dao.findFirst("select count(*) total from expert e where e.name like '%"+search+"%'");
        Long total = expert.getLong("total").longValue();
        map.put("total", total);
        arrts.add(map);
        return arrts;
    }

    public boolean ajaxUpdate(int id, String name, String ofCompany, String special, String address, String phone, String profile) {

        boolean result = Expert.dao.findById(id).set("name",name).set("ofCompany",ofCompany).set("special", special)
                .set("address", address).set("phone", phone).set("profile",profile).update();

        return result;
    }

    public boolean ajaxAdd(String name, String ofCompany, String special, String address, String phone, String profile) {

        // 检查是否重复
        Expert expert =  Expert.dao.findFirst("select count(*) count from expert where name = ?",name);
        Long count = expert.getLong("count").longValue();
        if(count > 0) {
            return false;
        }

        // 保存
        boolean result = new Expert().set("name",name).set("ofCompany",ofCompany).set("special", special)
                .set("address", address).set("phone", phone).set("profile",profile).save();
        return result;
    }

    public boolean ajaxDel(String id) {
        return Expert.dao.deleteById(id);
    }

}
